<?php
include_once 'ns_dao.class.php';

class ns_dao_chat
{
    public static function GetNewMessages($username)
    {
        $DB = ns_dao::create();
        $sql = "SELECT * FROM message WHERE MessageID > (SELECT LastSeenMessageID FROM user WHERE Username='{$username}')";
        $sql_result = $DB->query($sql);

        foreach ($sql_result as $key => $message_row)
            $result[$key] = ChatMessage::createFromArray($message_row);

        ns_dao_user::UpdateUsersLastSeenMessage($username);

        unset($DB);
        return $result;
    }

    public static function AddNewMessage($username, $message)
    {
        $currentTime = gmdate("Y-m-d H:i:s", time());
        $DB = ns_dao::create();
        $sql = "INSERT INTO message (Username, TimeSent, MessageContent) VALUES ('{$username}','{$currentTime}','{$message}')";
        $sql_result = $DB->DB->insert($sql);
        unset($DB);
    }
}

?>